# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import json
import re

if __name__ == '__main__':
    result_name = sys.argv[2]
    with open(result_name, 'r') as f:
        content = f.read()

    txt_data_list = [i.strip() for i in re.findall(r':(.*?),', content.replace('\n', ',') + ',')]
    fps = float(txt_data_list[7]) * 4
    latency = 1000 / float(txt_data_list[7])
    latency = '%.2f' % (latency)

    json_name = sys.argv[1]
    if 'BERT' in json_name:
        with open(json_name, 'r') as f:
            content = f.read()
        acc = [i.strip() for i in re.findall(r':(.*?),', content.replace('\n', ',') + ',')][-1]
        acc = float(acc)*100
        acc = '%.2f' % (acc) + '%'
        print('{},{},{}'.format(acc, fps, latency))
    else:
        with open(json_name, 'r') as f:
            content = f.read()
        Tops = [i.get('value') for i in json.loads(content).get('value') if 'Top' in i.get('key')]
        print('{},{},{},{}'.format(Tops[0], Tops[-1], fps, latency))
